package com.mes.inventory.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.mes.common.service.BasicService;
import com.mes.common.util.CodeUtil;
import com.mes.common.util.SysUtil;
import com.mes.inventory.dao.InvInfoDao;
import com.mes.inventory.dao.iface.IInvIODao;
import com.mes.inventory.dao.iface.IInvIODetailDao;
import com.mes.inventory.domain.InvIO;
import com.mes.inventory.domain.InvIODetail;
import com.mes.inventory.domain.InvInfo;
import com.mes.inventory.manager.InvInfoManager;

@Service
public class InvIODetailService extends BasicService<InvIODetail> {

	@Autowired
	CodeUtil codeUtil;

	@Autowired
	private IInvIODetailDao invIODetailDao;

	@Autowired
	private IInvIODao invIODao;

	@Autowired
	private InvInfoDao invInfoDao;

	@Autowired
	private InvInfoManager invlnfoManager;

	@DataProvider
	public Collection<InvIODetail> getInvIODetails(Map<String, Object> parameter) throws Exception {
		if (null != parameter) {
			String invIOUid = (String) parameter.get("invIOUid");
			return invIODetailDao.findByProperty("invIOUid", invIOUid);
		}
		return null;
	}

	// 多条件查询
	@DataProvider
	public void getInvIODetailsByConditions(Page<InvIODetail> page, Map<String, Object> parameter) throws Exception {
		if (null != parameter) {
			invIODetailDao.getInvIODetailByCondition(page, parameter);
		}
	}

	// 反冲登账
	@DataResolver
	public void saveMaterialRecoil(InvIO invIO, Collection<InvIODetail> invIODetails) throws Exception {
		// String warehouseUid = (String) parameter.get("warehouseUid");
		// Integer ioType = (Integer) parameter.get("ioType");
		// 创建反冲单
		InvIO invIO1 = new InvIO();
		invIO1.setUuid(SysUtil.getUUID());
		invIO1.setCreateTime(new Date());
		invIO1.setIoState(5);
		invIO1.setIoType(invIO.getIoType());
		if(invIO1.getIoType().toString().startsWith("1")){
			invIO1.setInvIOId(codeUtil.getInvInBillNumber());
		}else if(invIO1.getIoType().toString().startsWith("2")){
			invIO1.setInvIOId(codeUtil.getInvOutBillNumber());
		}
		invIO1.setIoDate(new Date());
		invIO1.setIoEmployeeUid(SysUtil.getSessionEmployeeUid());
		invIO1.setWarehouseUid(invIO.getWarehouseUid());
		invIO1.setCreatorUid(SysUtil.getSessionEmployeeUid());
		invIO1.setWarehouseManagerUid(invIO.getWarehouseManagerUid());
		Set<InvIODetail> set = new HashSet<InvIODetail>();
		List<InvIO> list = new ArrayList<InvIO>();
		int i = 1;
		for (InvIODetail detail : invIODetails) {
			String recoilNumStr = EntityUtils.getValue(detail, "recoilNum");
			float recoilNum = Integer.valueOf(recoilNumStr);
			set.add(detail);
			if (recoilNum < 0 && detail.getIoQty() + recoilNum >= 0) {
				detail.setIoQty(detail.getIoQty() + recoilNum);
				invIODetailDao.update(detail);
				// 创建反冲单明细
				InvIODetail iod = new InvIODetail();
				iod.setInvIOUid(invIO1.getUuid());
				iod.setMaterialUid(detail.getMaterialUid());
				iod.setPartDrawingId(detail.getPartDrawingId());
				iod.setPartName(detail.getPartName());
				iod.setPartDescription(detail.getPartDescription());
				iod.setWarebinUid(detail.getWarebinUid());
				iod.setBatchnum(detail.getBatchnum());
				iod.setSn(detail.getSn());
				iod.setIoQty(recoilNum);
				iod.setUom(detail.getUom());
				iod.setSeq(i);
				i++;
				invIODetailDao.add(iod);

			} else if (recoilNum < 0 && detail.getIoQty() + recoilNum < 0) {
				throw new Exception("反冲数不能多于原有数.请重新填写!");
			} else if (recoilNum > 0) {
				throw new Exception("反冲数必须为负数!");
			}
		}
		invIODao.add(invIO1);
		invIO.setDetails(set);
		list.add(invIO);
		invlnfoManager.updateInvInfo(list);
	}

	// 借用归还
	@DataResolver
	public void saveMaterialBack(InvIO inv, Collection<InvIODetail> invIODetails) throws Exception {
		Set<InvIODetail> set = new HashSet<InvIODetail>();
		List<InvIO> list = new ArrayList<InvIO>();
		int i = 1;
		for (InvIODetail detail : invIODetails) {
			String newBinId = EntityUtils.getValue(detail, "newBinId");
			// String warehouseUid = (String) parameter.get("warehouseUid");
			// detail.setIoQty(detail.getCurReturnQty());returnQty
			detail.setReturnDate(new Date());
			if (detail.getReturnQty() != null) {
				detail.setReturnQty(detail.getReturnQty() + detail.getCurReturnQty());
				detail.setIoQty(detail.getIoQty() - detail.getReturnQty() - detail.getCurReturnQty());
				if ((detail.getIoQty() - detail.getReturnQty() - detail.getCurReturnQty()) == 0) {
					detail.setIsFinishReturning(1);
				} else {
					detail.setIsFinishReturning(0);
				}
			} else {
				detail.setReturnQty(detail.getCurReturnQty());
				detail.setIoQty(detail.getIoQty() - detail.getCurReturnQty());
				if ((detail.getIoQty() - detail.getCurReturnQty()) == 0) {
					detail.setIsFinishReturning(1);
				} else {
					detail.setIsFinishReturning(0);
				}
			}
			set.add(detail);
			invIODetailDao.update(detail);
			// 创建归还单
			InvIO invIO = new InvIO();
			invIO.setUuid(SysUtil.getUUID());
			invIO.setInvIOId(codeUtil.getInvInBillNumber());
			invIO.setCreateTime(new Date());
			invIO.setIoState(5);
			// 归还入库
			invIO.setIoType(12);
			invIO.setIoDate(new Date());
			invIO.setIoEmployeeUid(SysUtil.getSessionEmployeeUid());
			invIO.setCreatorUid(SysUtil.getSessionEmployeeUid());
			invIO.setWarehouseManagerUid(inv.getWarehouseManagerUid());
			invIODao.add(invIO);
			// 创建明细
			InvIODetail iod = new InvIODetail();
			iod.setInvIOUid(invIO.getUuid());
			iod.setMaterialUid(detail.getMaterialUid());
			iod.setPartDrawingId(detail.getPartDrawingId());
			iod.setPartName(detail.getPartName());
			iod.setPartDescription(detail.getPartDescription());
			iod.setWarebinUid(newBinId);
			iod.setBatchnum(detail.getBatchnum());
			iod.setSn(detail.getSn());
			iod.setSeq(i);
			i++;
			iod.setUom(detail.getUom());
			if (detail.getReturnQty() != null) {
				iod.setReturnQty(detail.getReturnQty() + detail.getCurReturnQty());
				iod.setIoQty(detail.getIoQty() - detail.getReturnQty() - detail.getCurReturnQty());
				iod.setCurReturnQty(0f);
				if ((detail.getIoQty() - detail.getReturnQty() - detail.getCurReturnQty()) == 0) {
					iod.setIsFinishReturning(1);
				} else {
					iod.setIsFinishReturning(0);
				}
			} else {
				iod.setReturnQty(detail.getCurReturnQty());
				iod.setIoQty(detail.getIoQty() - detail.getCurReturnQty());
				iod.setCurReturnQty(0f);
				if ((detail.getIoQty() - detail.getCurReturnQty()) == 0) {
					iod.setIsFinishReturning(1);
				} else {
					iod.setIsFinishReturning(0);
				}
			}
			invIODetailDao.add(iod);
			inv.setDetails(set);
			list.add(inv);
			invlnfoManager.updateInvInfo(list);
		}
	}

}
